package com.example.demo.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.mapper.SaTokenUserMapper;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * sa-token控制器
 *
 * @author yzh
 * @date 2022/11/23
 */
@Controller
@RequestMapping("/satoken")
public class SaTokenController {
    @Autowired
    private SaTokenUserMapper saTokenUserMapper;

    /**
     * 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
     */

    @RequestMapping("doLogin")
    @ResponseBody
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        Integer id = saTokenUserMapper.selectIdByName(username);
        if ("root".equals(username) && "123".equals(password)) {

            StpUtil.login(id);
            return "登录成功";
        }
        return "登录失败";
    }

    /**
     * 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
     */
    @RequestMapping("isLogin")
    @ResponseBody
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @RequestMapping("/login")
    public String login() {
        return "satoken/login";
    }

}
